package com.yaozheng.vocationaltraining.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.iview.experience.IMyExperienceView;
import com.yaozheng.vocationaltraining.service.experience.MyExperienceService;
import com.yaozheng.vocationaltraining.service.impl.experience.MyExperienceServiceImpl;
import com.yaozheng.vocationaltraining.utils.ACache;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 我的经验值
 */
@EFragment(R.layout.fragment_my_experience)
public class MyExperienceFragment extends BaseFragment implements IMyExperienceView {
    @ViewById
    ImageView setUpImage, changeAvatarImageView, logoutImageView, userAvatarImage;
    @ViewById
    TextView headTitleText, maxIntegralText, quarterIntegralText;
    ACache aCache;
    BitmapLoader userBitmapLoader;
    @Bean(MyExperienceServiceImpl.class)
    MyExperienceService myExperienceService;
    JSONObject experienceJsonObject;

    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    /**
     * 初始化View
     */
    @AfterViews
    public void initView() {
        userBitmapLoader = new BitmapLoader(Glide.with(this), R.drawable.user_img);
        aCache = ACache.get(getContext());

        String userAvatar = aCache.getAsString("userAvatar");
        userBitmapLoader.displayImage(userAvatar, userAvatarImage);
        setUpImage.setVisibility(View.GONE);
        changeAvatarImageView.setVisibility(View.GONE);
        logoutImageView.setVisibility(View.GONE);
        myExperienceService.init(this);
        headTitleText.setText("我的经验值");
        loadData();
    }

    public void loadData() {
        updateData(null);
        myExperienceService.experienceData();
    }

    /**
     * 经验头衔
     */
    @Click(R.id.experienceDesignationText)
    public void experienceDesignationClick() {
        if (experienceJsonObject != null) {
            int experience = TypeUtils.getJsonInteger(experienceJsonObject, "experience", 0);
            getHomeActivity().openMyExperienceTitleFragment(experience);
        }
    }

    /**
     * 打开积分明细
     */
    @Click(R.id.experienceDetailsText)
    public void openIntegralDetailsFragment() {
        getHomeActivity().openIntegralDetailsFragment("经验值明细", 1);
    }

    public void updateData(JSONObject experienceJsonObject) {
        maxIntegralText.setText(String.valueOf(TypeUtils.getJsonInteger(experienceJsonObject, "experience", 0)));
        quarterIntegralText.setText(String.valueOf(TypeUtils.getJsonInteger(experienceJsonObject, "quarterlyExperience", 0)));

    }

    @UiThread
    @Override
    public void experienceDataSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        experienceJsonObject = TypeUtils.getJsonObject(jsonObject, "data", "experience");
        updateData(experienceJsonObject);
    }

    @UiThread
    @Override
    public void experienceDataError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }
}
